package com.bosssoft.oss.service.impl;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.bosssoft.oss.dao.FileDao;
import com.bosssoft.oss.utils.OssServer;
import com.bosssoft.oss.pojo.FileUpAndDown;
import com.bosssoft.oss.service.ThreadService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.*;

@Service
public class ThreadServiceImpl implements ThreadService {
    @Autowired
    private OssServer ossServer;
    @Autowired
    private OSSClient ossClient;
    @Autowired
    private FileDao fileDao;
    //创建线程池
    ExecutorService threadPool =new ThreadPoolExecutor(
            2,//核心线程池
            5,//最大线程数
            3,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque(3),//候客区数量
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.CallerRunsPolicy()
    );
    @Async
    @Override
    public FileUpAndDown upload(MultipartFile[] multipartFiles) {

        FileUpAndDown fileUpAndDown = new FileUpAndDown();
        for (MultipartFile multipartFile : multipartFiles) {
            String fileName = multipartFile.getOriginalFilename();
            String filePath = getFilePath(fileName);
            File file = fileDao.getFile(filePath);
            try {

                threadPool.execute(() -> {
                    ossClient.putObject(ossServer.getBucketName(), fileName, file);
                });

            } catch (Exception e) {
                e.printStackTrace();

                fileUpAndDown.setStatus("failed");

                return fileUpAndDown;
            } finally {
                threadPool.shutdown();//关闭线程池
            }
            fileUpAndDown.setStatus("over");

            fileUpAndDown.setResult("success");

            fileUpAndDown.setFname(ossServer.getUrlPrefix() + filePath);

            fileUpAndDown.setFid(String.valueOf(System.currentTimeMillis()));

        }
        return fileUpAndDown;
}
    @Async
    @Override
    public void download(OSSClient ossClient, String objectName,String path) throws IOException {

        try{
            threadPool.execute(()->{

             fileDao.saveFile(ossClient,ossServer.getBucketName(),path,objectName);

            });
        }catch (Exception e){

        }finally {
          threadPool.shutdown();//关闭线程池
        }



    }
    @Async
    @Override
    public String getFilePath(String fileName) {
        //获取文件后缀名
        String ext=fileName.substring(fileName.lastIndexOf("."));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        String date = sdf.format(new Date());

        String uuid = UUID.randomUUID().toString().replace("-", "");

        return "document/"+date+"/"+uuid+ext;
    }
}
